package org.testis.utils;

import java.io.UnsupportedEncodingException;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author nurhat
 */
public class StringUtils {

    public static String stringToHex(String input, String charsetName) throws UnsupportedEncodingException, UnsupportedEncodingException {
        if (input == null) {
            throw new NullPointerException();
        }
        return asHex(input.getBytes(charsetName));
    }
    private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();

    public static String asHex(byte[] buf) {
        char[] chars = new char[2 * buf.length];
        for (int i = 0; i < buf.length; ++i) {
            chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4];
            chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F];
        }
        return new String(chars);
    }
    public static String ClearHTMLTags(String strHTML,
            int intWorkFlow) {
        return HTMLParser.ClearHTMLTags(strHTML,
                intWorkFlow);
    }
    /* remove leading whitespace */

    public static String ltrim(String source) {
        return BlankRemover.ltrim(source);
    }

    /* remove trailing whitespace */
    public static String rtrim(String source) {
        return BlankRemover.rtrim(source);
    }

    /* replace multiple whitespaces between words with single blank */
    public static String itrim(String source) {
        return BlankRemover.itrim(source);
    }

    /* remove all superfluous whitespaces in source string */
    public static String trim(String source) {
        return BlankRemover.trim(source);
    }
    /* remove all  */

    public static String trimAll(String source) {
        source = BlankRemover.trim(source);
        source = BlankRemover.itrim(source);
        source = BlankRemover.ltrim(source);
        source = BlankRemover.rtrim(source);
        source = BlankRemover.lrtrim(source);
        return source;
    }

    public static String removeSpaces(String source) {
        return BlankRemover.removeSpaces(source);
    }

    public static String lrtrim(String source) {
        return BlankRemover.lrtrim(source);
    }
}

class HTMLParser {

    public static String ClearHTMLTags(String strHTML,
            int intWorkFlow) {
        Pattern pattern = null;
        Matcher matcher = null;
        String regex;
        String strTagLess = null;
        strTagLess = strHTML;

        if (intWorkFlow == -1) {
            regex = "<[^>]*>";
            //removes all html tags
            pattern = Pattern.compile(regex);
            strTagLess = pattern.matcher(strTagLess).replaceAll(" ");
        }

        if (intWorkFlow > 0 && intWorkFlow < 3) {

            regex = "[<]";
            //matches a single <
            pattern = Pattern.compile(regex);
            strTagLess = pattern.matcher(strTagLess).replaceAll("<");

            regex = "[>]";
            //matches a single >	
            pattern = Pattern.compile(regex);
            strTagLess = pattern.matcher(strTagLess).replaceAll(">");
        }
        return strTagLess;
    }
}

class BlankRemover {

    /* remove leading whitespace */
    public static String ltrim(String source) {
        return source.replaceAll("^\\s+",
                "");
    }

    /* remove trailing whitespace */
    public static String rtrim(String source) {
        return source.replaceAll("\\s+$",
                "");
    }

    /* replace multiple whitespaces between words with single blank */
    public static String itrim(String source) {
        return source.replaceAll("\\b\\s{2,}\\b",
                " ");
    }

    /* remove all superfluous whitespaces in source string */
    public static String trim(String source) {
        return itrim(ltrim(rtrim(source)));
    }

    public static String lrtrim(String source) {
        return ltrim(rtrim(source));
    }

    public static String removeSpaces(String s) {
        StringTokenizer st = new StringTokenizer(s,
                " ",
                false);
        String t = "";
        while (st.hasMoreElements()) {
            t += st.nextElement();
        }
        return t;
    }
}